package com.jlect.swebing.ui.client;

/**
 * Lays out components using 5 components layouting - four part of earth and center.
 * @author Sergey Kozmin
 * @since 13.11.2007 10:38:55
 */
public class BorderLayout implements LayoutManager {
    private int hgap;
    private int vgap;

    private GComponent north;
    private GComponent west;
    private GComponent east;
    private GComponent south;
    private GComponent center;

    /**
     * The north layout constraint
     */
    public static final LayoutConstraint NORTH = BorderLayoutConstraint.NORTH;

    /**
     * The south layout constraint
     */
    public static final LayoutConstraint SOUTH = BorderLayoutConstraint.SOUTH;

    /**
     * The east layout constraint
     */
    public static final LayoutConstraint EAST = BorderLayoutConstraint.EAST;

    /**
     * The west layout constraint
     */
    public static final LayoutConstraint WEST = BorderLayoutConstraint.WEST;

    /**
     * The center layout constraint
     */
    public static final LayoutConstraint CENTER = BorderLayoutConstraint.CENTER;

    public BorderLayout() {
        this(0, 0);
    }

    public BorderLayout(int hgap, int vgap) {
        this.hgap = hgap;
        this.vgap = vgap;
    }

    public int getHgap() {
        return hgap;
    }

    public void setHgap(int hgap) {
        this.hgap = hgap;
    }

    public int getVgap() {
        return vgap;
    }

    public void setVgap(int vgap) {
        this.vgap = vgap;
    }

    public void addLayoutComponent(GComponent comp, LayoutConstraint constraint) {
        if (constraint != null && constraint instanceof BorderLayoutConstraint) {
            if (CENTER.equals(constraint)) {
                center = comp;
            } else if (NORTH.equals(constraint)) {
                north = comp;
            } else if (SOUTH.equals(constraint)) {
                south = comp;
            } else if (EAST.equals(constraint)) {
                east = comp;
            } else if (WEST.equals(constraint)) {
                west = comp;
            } else {
                throw new IllegalArgumentException("Could not find appropriate constraint [" + constraint.toString() + "]. ");
            }
        } else {
            throw new IllegalArgumentException("Constraint must be not null and be BorderLayoutContstraint " +
                    "type current constraint is [" + constraint + "]. ");
        }
    }

    public void removeLayoutComponent(GComponent comp) {
        if (comp == center) {
            center = null;
        } else if (comp == north) {
            north = null;
        } else if (comp == south) {
            south = null;
        } else if (comp == east) {
            east = null;
        } else if (comp == west) {
            west = null;
        }
    }

    public GComponent getLayoutComponent(LayoutConstraint constraint) {
        if (CENTER.equals(constraint)) {
            return center;
        } else if (NORTH.equals(constraint)) {
            return north;
        } else if (SOUTH.equals(constraint)) {
            return south;
        } else if (WEST.equals(constraint)) {
            return west;
        } else if (EAST.equals(constraint)) {
            return east;
        } else {
            throw new IllegalArgumentException("Could not find appropriate constraint [" + constraint + "]. ");
        }
    }

    public LayoutConstraint getConstraints(GComponent comp) {
        if (comp == null) {
            return null;
        }
        if (comp == center) {
            return CENTER;
        } else if (comp == north) {
            return NORTH;
        } else if (comp == south) {
            return SOUTH;
        } else if (comp == west) {
            return WEST;
        } else if (comp == east) {
            return EAST;
        }
        return null;
    }

    public Dimension minimumLayoutSize(GContainer target) {
        Dimension dim = new Dimension(0, 0);

        if (east != null) {
            Dimension d = east.getMinimumSize();
            dim.width += d.width + hgap;
            dim.height = Math.max(d.height, dim.height);
        }
        if (west != null) {
            Dimension d = west.getMinimumSize();
            dim.width += d.width + hgap;
            dim.height = Math.max(d.height, dim.height);
        }
        if (center != null) {
            Dimension d = center.getMinimumSize();
            dim.width += d.width;
            dim.height = Math.max(d.height, dim.height);
        }
        if (north != null) {
            Dimension d = north.getMinimumSize();
            dim.width = Math.max(d.width, dim.width);
            dim.height += d.height + vgap;
        }
        if (south != null) {
            Dimension d = south.getMinimumSize();
            dim.width = Math.max(d.width, dim.width);
            dim.height += d.height + vgap;
        }

        return dim;
    }

    public Dimension preferredLayoutSize(GContainer target) {
        Dimension dim = new Dimension(0, 0);

        if (east != null) {
            Dimension d = east.getPreferredSize();
            dim.width += d.width + hgap;
            dim.height = Math.max(d.height, dim.height);
        }
        if (west != null) {
            Dimension d = west.getPreferredSize();
            dim.width += d.width + hgap;
            dim.height = Math.max(d.height, dim.height);
        }
        if (center != null) {
            Dimension d = center.getPreferredSize();
            dim.width += d.width;
            dim.height = Math.max(d.height, dim.height);
        }
        if (north != null) {
            Dimension d = north.getPreferredSize();
            dim.width = Math.max(d.width, dim.width);
            dim.height += d.height + vgap;
        }
        if (south != null) {
            Dimension d = south.getPreferredSize();
            dim.width = Math.max(d.width, dim.width);
            dim.height += d.height + vgap;
        }

        return dim;
    }

    public void layoutContainer(GContainer target) {
        int top = 0;
        int bottom = target.getHeight();
        int left = 0;
        int right = target.getWidth();

        if (north != null) {
            Dimension d = north.getPreferredSize();
            north.setBounds(left, top, right - left, d.height);
            top += d.height + vgap;
        }
        if (south != null) {
            Dimension d = south.getPreferredSize();
            south.setBounds(left, bottom - d.height, right - left, d.height);
            bottom -= d.height + vgap;
        }
        if (east != null) {
            Dimension d = east.getPreferredSize();
            east.setBounds(right - d.width, top, d.width, bottom - top);
            right -= d.width + hgap;
        }
        if (west != null) {
            Dimension d = west.getPreferredSize();
            west.setBounds(left, top, d.width, bottom - top);
            left += d.width + hgap;
        }
        if (center != null) {
            center.setBounds(left, top, right - left, bottom - top);
        }
    }

    private static class BorderLayoutConstraint implements LayoutConstraint {
        public static final BorderLayoutConstraint NORTH = new BorderLayoutConstraint("NORTH");
        public static final BorderLayoutConstraint SOUTH = new BorderLayoutConstraint("SOUTH");
        public static final BorderLayoutConstraint WEST = new BorderLayoutConstraint("WEST");
        public static final BorderLayoutConstraint EAST = new BorderLayoutConstraint("EAST");
        public static final BorderLayoutConstraint CENTER = new BorderLayoutConstraint("CENTER");

        private String name;

        private BorderLayoutConstraint(String name) {
            this.name = name;
        }

        public String toString() {
            return "BorderLayoutContstraint{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}
